package com.example.publishDemo.Controller;

import com.example.publishDemo.Mapper.UserMapper;
import com.example.publishDemo.Model.AjaxResult;
import com.example.publishDemo.Model.Article;
import com.example.publishDemo.Model.ArticleReport;
import com.example.publishDemo.Model.User;
import com.example.publishDemo.Service.AdminService;
import com.example.publishDemo.Service.ArticleService;
import com.example.publishDemo.Tools.Tool;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    AdminService adminService;


    @GetMapping("/parameter")
    public AjaxResult parameter(){
        Map<String, Object> map = new HashMap<>();

        int totalArticle = adminService.totalArticle();
        int totalUser = adminService.totalUser();
        int totalComment = adminService.totalComment();
        int totalView = adminService.totalView();
        map.put("totalArticle", totalArticle);
        map.put("totalUser", totalUser);
        map.put("totalComment", totalComment);
        map.put("totalView", totalView);
        return AjaxResult.success(map);
    }

    @GetMapping("/articleType")
    public AjaxResult articleType(){
        Map<String, Object> map = new HashMap<>();
        map.put("type", "pie");
            Map<String, String> text = new HashMap<>();
            text.put("text", "文章分类统计");
        map.put("title", text);
            Map<String, String> legend = new HashMap<>();
            legend.put("position", "left");
        map.put("legend", legend);
        map.put("bgColor", "#fbfbfb");
        map.put("labels", adminService.getAllTagName());

        //// 取出每个Tag标签下的文章数
        List<Object> datasets = new ArrayList<>();
            Map<String, Object> data = new HashMap<>();
                List<Integer> collect = adminService.getAllTagName().stream().map(tagName -> {
                    int articleForTagName = adminService.findArticleForTagName(tagName);
                    return articleForTagName;
                }).collect(Collectors.toList());
                data.put("data", collect);
            datasets.add( data);
        map.put("datasets", datasets);
        return AjaxResult.success(map);
    }

    @GetMapping("/articleChart")
    public AjaxResult articleChart(){
        Map<String, Object> map = new HashMap<>();
        map.put("type", "bar");
            Map<String, String> text = new HashMap<>();
            text.put("text", "文章浏览量排行");
        map.put("title", text);
        map.put("bgColor", "#fbfbfb");
        map.put("labels", adminService.getArticleTop5Title());

        // 取出Top5文章的浏览量
        List<Object> datasets = new ArrayList<>();
            Map<String, Object> label = new HashMap<>();
                label.put("label", "Top5排行榜");
                label.put("fillColor", "rgba(241, 49, 74, 0.5)");
                label.put("data",adminService.getArticleTop5Views());
        datasets.add(label);
        map.put("datasets",datasets);
        return AjaxResult.success(map);
    }

    @GetMapping("/visition")    //严格按照文档结构返回参数:https://lin-xin.gitee.io/example/schart/bar.html#%E4%BD%BF%E7%94%A8%E6%A1%88%E4%BE%8B
    public AjaxResult visition(){
        Map<String, Object> map = new HashMap<>();
        map.put("type", "line");
            Map<String, String> text = new HashMap<>();
            text.put("text", "最近一周访问量势图");
        map.put("title", text);
        map.put("bgColor", "#fbfbfb");
        map.put("labels", Tool.getPastSevenDays());

        // 取出过去七天的访问数值
        String dailyView = String.format("daily-views");
        List<Object> datasets = new ArrayList<>();
            Map<String, Object> label = new HashMap<>();
                List<Double> data = new ArrayList<>();
                    List<String> pastSevenDays = Tool.getPastSevenDays();

        label.put("label", "访问数");
        for (String day : pastSevenDays) {
            String keyForDay = String.format("daily-views:%s", day);
            Double views = redisTemplate.opsForZSet().score(dailyView, keyForDay);
            data.add(views != null ? views : 0);
        }
        label.put("data",data);
        datasets.add(label);

        map.put("datasets", datasets);
        return AjaxResult.success(map);
    }

    @GetMapping("/unViolation")
    public AjaxResult unViolation(@RequestParam int r_id) {
        adminService.completeProcess(r_id);
        return AjaxResult.success(true);
    }

    @GetMapping("/violation")
    public AjaxResult violation(@RequestParam int a_id,int r_id) {
        adminService.delArticleForId(a_id);
        adminService.completeProcess(r_id);
        return AjaxResult.success(true);
    }

    @GetMapping("/getReport")
    public AjaxResult getReport(@RequestParam int pageNum, int size) {
        int totalReport = adminService.totalReport();

        if (pageNum < 1) {
            pageNum = 1;
        }else if(pageNum > totalReport / size){
            pageNum = (int) Math.ceil((double) totalReport / size);
        }else {
            pageNum = pageNum;
        }

        int start = (pageNum - 1) * size;
        int end = start + size ;
        if(start > totalReport){
            start = totalReport - 1;
            end = totalReport;
        }

        List<ArticleReport> list = adminService.ReportRange(start, end);
        List<User> users = list.stream().map(articleReport -> adminService.getUserById(articleReport.getU_id())).collect(Collectors.toList());
        List<Article> articles = list.stream().map(articleReport -> adminService.getArticleForId(articleReport.getA_id())).collect(Collectors.toList());

        List<Object> data = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> respData = new HashMap<>();
            respData.put("articleReport", list.get(i));
            respData.put("user", users.get(i));
            respData.put("article", articles.get(i));
            data.add(respData);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("total", totalReport);
        map.put("pageNum", pageNum);
        map.put("size", size);
        map.put("data", data);

        return AjaxResult.success(map);
    }

    @GetMapping("/Admin")
    public AjaxResult Admin(@RequestParam int u_id) {
        boolean recommend = adminService.isAdmin(u_id);
        if (recommend) {
            adminService.unAdmin(u_id);
            return AjaxResult.success(false);
        }
        adminService.Admin(u_id);
        return AjaxResult.success(true);
    }

    @GetMapping("/Recommend")
    public AjaxResult Recommend(@RequestParam int a_id) {
        boolean recommend = adminService.isRecommend(a_id);
        if (recommend) {
            adminService.unRecommend(a_id);
            return AjaxResult.success(false);
        }
        adminService.Recommend(a_id);
        return AjaxResult.success(true);
    }

    @GetMapping("/delUser")
    public AjaxResult delUser(@RequestParam int u_id) {
        adminService.delUserForId(u_id);
        return AjaxResult.success(true);
    }

    @GetMapping("/getUser")
    public AjaxResult getUser(@RequestParam int pageNum, int size) {
        int totalUser = adminService.totalUser();

        if (pageNum < 1) {
            pageNum = 1;
        }else if(pageNum > totalUser / size){
            pageNum = (int) Math.ceil((double) totalUser / size);
        }else {
            pageNum = pageNum;
        }

        int start = (pageNum - 1) * size;
        int end = start + size ;
        if(start > totalUser){
            start = totalUser - 1;
            end = totalUser;
        }

        List<User> list = adminService.UserRange(start, end);
        Map<String, Object> map = new HashMap<>();
        map.put("total", totalUser);
        map.put("pageNum", pageNum);
        map.put("size", size);
        map.put("data", list);
        return AjaxResult.success(map);
    }

    @GetMapping("/delArticle")
    public AjaxResult delArticle(@RequestParam int a_id) {
        adminService.delArticleForId(a_id);
        return AjaxResult.success(true);
    }

    @GetMapping("/getArticle")
    public AjaxResult getArticle(@RequestParam int pageNum, int size) {
        int totalArticle = adminService.totalArticle();

        if (pageNum < 1) {
            pageNum = 1;
        }else if(pageNum > totalArticle / size){
            pageNum = (int) Math.ceil((double) totalArticle / size);
        }else {
            pageNum = pageNum;
        }

        int start = (pageNum - 1) * size;
        int end = start + size ;
        if(start > totalArticle){
            start = totalArticle - 1;
            end = totalArticle;
        }

        List<Article> list = adminService.articleRange(start, end);
        Map<String, Object> map = new HashMap<>();
        map.put("total", totalArticle);
        map.put("pageNum", pageNum);
        map.put("size", size);
        map.put("data", list);
        return AjaxResult.success(map);
    }
}
